#include "mainwindow.h"
#include "scene/splashscene.h"
#include "resources.h"
#include "commentdef.h"
#include <QEvent>
#include "scene/gamescene.h"
#include "sound/GameSounds.h"

MainWindow::MainWindow(QWidget *parent)
    : QGraphicsView(parent)
{
    resources::m_mainwindow = this;

    setFrameShape(QFrame::NoFrame);
    setRenderHints(QPainter::HighQualityAntialiasing | QPainter::SmoothPixmapTransform);
    setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);

    setGeometry(0.0,0.0,360.0,640.0);

    setViewportUpdateMode(QGraphicsView::NoViewportUpdate);

    m_timer.setInterval(UI_INTERVAL);
    connect(&m_timer,SIGNAL(timeout()),this,SLOT(rendering()));
    m_timer.start();
    resources::SetSplash();
}

MainWindow::~MainWindow()
{

}

void MainWindow::rendering()
{
    update();
}

bool MainWindow::event(QEvent *e)
{
    if(e->type() == QEvent::ActivationChange)
    {
        if(this->isActiveWindow())
        {
            qWarning("in?");
            static bool first = true;
            if(!first)
            {
                if(scene() != resources::m_gamescene)
                {
                    if(resources::m_bplaysound)
                        resources::m_sound->playBgSound(0);
                }
            }
            first = false;
        }
        //out
        else
        {
            qWarning("out");
            static bool first = true;
            if(!first)
            {

            }
            if(scene() != resources::m_gamescene)
            {
                if(resources::m_bplaysound)
                    resources::m_sound->stop(0);
            }
            else
            {
                resources::m_gamescene->pause();
            }
            first = false;
        }

        return true;
    }
    else
    {
        return QGraphicsView::event(e);
    }

}
